package com.watchthelan.service.server;
import org.springframework.validation.Validator;
import org.springframework.validation.Errors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.watchthelan.domain.Server;

public class ServerValidator implements Validator {

    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    public boolean supports(Class clazz) {
        return Server.class.equals(clazz);
    }

    public void validate(Object obj, Errors errors) {
    	Server server = (Server) obj;
    	
    	if (!com.watchthelan.util.Validator.validHostname(server.getHostname())
    			&& ! com.watchthelan.util.Validator.validIp(server.getHostname())) {
    		errors.rejectValue("hostname", "error.server.hostname", null, "Value required.");
    	}
    	
    	if (0 == server.getPort()) {
    		errors.rejectValue("port", "error.server.port", null, "Value required.");
    	} 
    }
}